package com.voltmao.controller.module.doc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.voltmao.service.application.doc.DocDbServiceImpl;
import com.voltmao.service.application.doc.DocTypeServiceImpl;
import com.voltmao.webpro.frame.analyze.DTOSmith;
import com.voltmao.webpro.frame.dto.ApiResult;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.frame.dto.GridViewResult;
import com.voltmao.webpro.frame.dto.status.Constants;
import com.voltmao.webpro.frame.exception.WebProViewException;
import com.voltmao.webpro.utils.http.HttpRequestUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 公司资料文档控制层
 */
@Controller
@RequestMapping(value = "/module/doc/db")
public class DocDbController {
    @Autowired
    DocDbServiceImpl docDbService;
    @Autowired
    DocTypeServiceImpl docTypeService;
    /**
     * 新增或更新文档
     * @param request
     * @return
     */
    @RequestMapping(value = "/insertOrUpdate.json")
    @ResponseBody
    public ApiResult insertOrUpdate(HttpServletRequest request){
        return new DTOSmith(request).builderApiResult(new DTOSmith.ApiLoadBackCall() {
            @Override
            public Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                try{
                    return docDbService.insertOrUpdate(params);
                }catch (Exception e){
                    apiResult.setStatus(Constants.PARAMETER_ERROR);
                    return e.getMessage();
                }
            }
        });
    }

    @RequestMapping(value = "/initGrid.json")
    @ResponseBody
    public GridViewResult initGrid(HttpServletRequest request){
        return new DTOSmith(request).builderLoadGridViewResult(new DTOSmith.LoadGridViewBackCall() {
            @Override
            public String getDataUrl(HttpServletRequest request) {
                return "/sear.json";
            }

            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);

                return docDbService.getDocs(params);
            }

            @Override
            public Object defalutForm(HttpServletRequest request) throws WebProViewException {
                Map<String,Object> res = new HashMap<>(8);
                res.put("docType",docTypeService.getAllDocType());
                return res;
            }
        });
    }

    @RequestMapping(value = "/sear.json")
    @ResponseBody
    public GridViewResult sear(HttpServletRequest request){
        return new DTOSmith(request).builderSeachGridResult(new DTOSmith.SeachGridBackCall() {
            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                apiResult.dataUrl="/sear.json";
                return docDbService.getDocs(params);
            }
        });
    }
}
